package com.example.demo.controller;
import com.example.demo.entity.CrCustomer;
import com.example.demo.entity.CrRepairOrder;
import com.example.demo.entity.CrSalesAccount;
import com.example.demo.entity.InteInquire;
import com.example.demo.service.CrCustomerService;
import com.example.demo.service.CrSalesAccountService;
import com.example.demo.service.RepairOrderService;
import com.example.util.FileUpUtil;
import com.example.util.JsonResult;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 *
 * 维修清单
 */
@Controller
@RequestMapping("/repair_order")

public class RepairOrderController {

    private Logger logger = LoggerFactory.getLogger(CrRepairOrder.class);

    private String url = "";

    private HttpServletRequest requestUrl;

    @Autowired
    private RepairOrderService inteInquireService;

    @Autowired
    private CrCustomerService crCustomerService;

    @Autowired
    private CrSalesAccountService crSalesAccountService;

    private MultipartFile[] multipartfiles;

    private List<MultipartFile> Mullist = new ArrayList<MultipartFile>();

    @RequestMapping("/")
    public JsonResult accept(HttpServletRequest request){

        return new JsonResult();
    }

    /**
     * 积分查询
     * @param name 用户表客户名称
     * @param car 车牌号码
     * @return
     */
    @RequestMapping("/inquire_integral")
    @ResponseBody
    public JsonResult inquireIntegral(String name,String car){

        List<InteInquire> list = inteInquireService.selectAll(name,car);

        return new JsonResult(list);
    }

    /**
     * 财务报表查询
     * @param names 用户表客户名称
     * @param carNum 车牌号码
     * @return
     */
    @RequestMapping("/inquire_reports")
    @ResponseBody
    private JsonResult inquireReports(String names,String carNum){
        List<InteInquire> list1 = inteInquireService.selectReport(names,carNum);
        return new JsonResult(list1);
    }

    /**
     * 维修清单页面查询条件
     * @param request
     * @return
     */
    @RequestMapping("/repair_find")
    @ResponseBody
    public JsonResult repairFind(HttpServletRequest request){

        String result = request.getParameter("result");


        return null;
    }
    @RequestMapping(value="/image_upload")
    @ResponseBody
    public String imageUpload(MultipartHttpServletRequest request){
        try {
            url = new File("").getCanonicalPath()+"\\src\\main\\resources\\static\\master\\images\\uploadIges\\";//获取当前工程路径
            MultipartFile file = request.getFile("file");
            if(!file.isEmpty()){
                new FileUpUtil().multiUpload(file,url);//=====图片上传方法
            }else{
                logger.info("文件为空...无法上传...");
            }
        } catch (IOException e) {
            e.printStackTrace();
            logger.error("文件上传失败...");
        }
        return "";
    }

    /**
     * 维修清单插入数据
     * @param
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/repair_insert")
    public String testPicUpload(String addForm,String saleForm) {
        String partsId = UUID.randomUUID().toString().replace("-","");
        String otherId = UUID.randomUUID().toString().replace("-","");
        Date date = new Date();
        BigDecimal totlePrice = new BigDecimal("0");
       /* String addForm = request.getParameter("addForm");
        String saleForm = request.getParameter("saleForm");*/
        CrRepairOrder crOrder = new CrRepairOrder();//维修清单
        CrCustomer crCustomer = new CrCustomer();//客户实体
        FileUpUtil fileUpUtil = new FileUpUtil();
        try{
            url = new File("").getCanonicalPath()+"\\src\\main\\resources\\static\\master\\images\\uploadIges\\";//获取当前工程路径
            //销货清单插入数据库操作
            if(saleForm!=""||saleForm!=null){
                JSONArray jSONArray = JSONArray.fromObject(saleForm);
                List<CrSalesAccount> list = (List<CrSalesAccount>) jSONArray.toCollection(jSONArray, CrSalesAccount.class);
                for(int i=0;i<list.size();i++){
                    CrSalesAccount crSales = new CrSalesAccount();//销货清单
                    crSales.setId(otherId);//销货清单Id
                    crSales.setPartsOrderId(partsId);//关联维修清单Id
                    crSales.setPartsname(list.get(i).getPartsname());//配件名称
                    crSales.setPartsnum(list.get(i).getPartsnum());//配件数量
                    crSales.setPartsunit(list.get(i).getPartsunit());//配件单位
                    crSales.setPartsprice(list.get(i).getPartsprice());//配件单价
                    crSales.setPartstotal(list.get(i).getPartstotal());//总价
                    crSales.setCreateTime(date);//时间
                    totlePrice.add(list.get(i).getPartstotal());//计算维修清单总价
                    crSalesAccountService.insertAccount(crSales);
                }
            }
            //维修清单插入数据库操作
            if(addForm!=""||addForm!=null){
                JSONObject jsonObject = JSONObject.fromObject(addForm);
                crOrder.setId(partsId);//维修清单Id
                crOrder.setCustomerid(otherId);//关联客户Id
                crOrder.setCustomername(jsonObject.getString("clientName")); //客户名称
                crOrder.setPhone(jsonObject.getString("clientPhone"));//客户电话
                crOrder.setCarnumber(jsonObject.getString("clientCarCode"));//车牌号
                crOrder.setBrand(jsonObject.getString("clientCarBrand"));//车辆品牌
                crOrder.setVin(jsonObject.getString("clientShelfCode"));//车架号
                crOrder.setCarage(jsonObject.getString("clientCarYear"));//车辆年限
                crOrder.setEmission(jsonObject.getString("clientCarPlace"));//车辆排量
                crOrder.setMaintenanctypes(jsonObject.getString("clientCarType"));//维修类型
                crOrder.setRunrange(jsonObject.getString("clientCarKilo"));//已驶公里数
                crOrder.setAgent(jsonObject.getString("clientCarAgent"));//经办人
                crOrder.setRepairpeople(jsonObject.getString("clientCarMain"));//维修人
                crOrder.setPaymethod(Integer.parseInt(jsonObject.getString("clientCarPay")));//付款方式
                crOrder.setTotalprice(totlePrice);
                long lon = Long.parseLong(jsonObject.getString("clientCarTime"));
                date.setTime(lon);
                crOrder.setCreatetime(date);//维修时间
                crOrder.setRemark(jsonObject.getString("clientCarRemark"));//备注信息
            /*添加客户表*/
                crCustomer.setId(otherId);//客户Id
                crCustomer.setCustomername(jsonObject.getString("clientName"));//客户姓名
                crCustomer.setPhone(jsonObject.getString("clientPhone"));//客户电话
                crCustomer.setCarnumber(jsonObject.getString("clientCarCode"));//车牌号
                crCustomer.setCreatetime(date);//创建日期
                crCustomer.setRemark(jsonObject.getString("clientCarRemark"));//备注
                int y = crCustomerService.inseCustomer(crCustomer);
            }
        }catch(Exception ex){
            System.out.println("异常：===+ "+ex);
        }

        return "suc";
    }

    @RequestMapping("/select_allOrder")
    @ResponseBody
    public JsonResult selectAllOrder(){
        List<CrRepairOrder> allOrder = inteInquireService.selectAllOrder();
        return new JsonResult(allOrder);
    }

}
